package com.wanmait.employment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wanmait.employment.entity.*;
import com.wanmait.employment.mapper.*;
import com.wanmait.employment.service.InterviewInvitationService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Base64;
import java.util.List;


@Service
public class InterviewInvitationServiceImpl implements InterviewInvitationService {
    @Resource
    private InterviewInvitationMapper interviewInvitationMapper;
    @Resource
    private UserinfoMapper userinfoMapper;
    @Resource
    private RecruitmentEducationMapper educationMapper;
    @Resource
    private WorkExperienceMapper workExperienceMapper;
    @Resource
    private StateMapper stateMapper;
    @Resource
    private PositionMapper positionMapper;
    @Resource
    private ExpectedSalaryMapper expectedSalaryMapper;
    @Resource
    private NatureWorkMapper natureWorkMapper;
    @Resource
    private AddressMapper addressMapper;
    @Resource
    private RecruitmentPostMapper recruitmentPostMapper;


    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public List<InterviewInvitation> findList(Integer publisherId) {
        QueryWrapper<InterviewInvitation> interviewInvitationQueryWrapper=new QueryWrapper<>();
        interviewInvitationQueryWrapper.inSql("recruitment_post_id","select publisher_id from recruitment_post where publisher_id = "+publisherId)
                .eq("agree",true);
        interviewInvitationQueryWrapper.orderByDesc("sending_time");
        List<InterviewInvitation> interviewInvitations = interviewInvitationMapper.selectList(interviewInvitationQueryWrapper);
        interviewInvitations.forEach(interviewInvitation -> {
            LambdaQueryWrapper<RecruitmentPost> recruitmentPostLambdaQueryWrapper=new LambdaQueryWrapper<>();
            recruitmentPostLambdaQueryWrapper.eq(RecruitmentPost::getId,interviewInvitation.getRecruitmentPostId());
            RecruitmentPost recruitmentPost=recruitmentPostMapper.selectOne(recruitmentPostLambdaQueryWrapper);
            //期望薪资
            LambdaQueryWrapper<ExpectedSalary> expectedSalaryQueryWrapper=new LambdaQueryWrapper<>();
            expectedSalaryQueryWrapper.eq(ExpectedSalary::getId,recruitmentPost.getExpectedSalaryId());
            recruitmentPost.setExpectedSalary(expectedSalaryMapper.selectOne(expectedSalaryQueryWrapper));
            interviewInvitation.setRecruitmentPost(recruitmentPost);
            //联动工作经验
            LambdaQueryWrapper<WorkExperience> workExperienceQueryWrapper=new LambdaQueryWrapper<>();
            workExperienceQueryWrapper.eq(WorkExperience::getId,recruitmentPost.getWorkExperienceId());
            recruitmentPost.setWorkExperience( workExperienceMapper.selectOne(workExperienceQueryWrapper));
            interviewInvitation.setRecruitmentPost(recruitmentPost);
            //工作性质
            LambdaQueryWrapper<NatureWork> natureWorkQueryWrapper=new LambdaQueryWrapper<>();
            natureWorkQueryWrapper.eq(NatureWork::getId,recruitmentPost.getNatureWorkId());
            recruitmentPost.setNatureWork( natureWorkMapper.selectOne(natureWorkQueryWrapper));
            interviewInvitation.setRecruitmentPost(recruitmentPost);
            //查找个人简历
            LambdaQueryWrapper<Userinfo> userinfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            userinfoLambdaQueryWrapper.eq(Userinfo::getId, interviewInvitation.getUserinfoId());
            Userinfo userinfo = userinfoMapper.selectOne(userinfoLambdaQueryWrapper);

//            // 获取面试邀请关联的图片数据，假设数据类型为String
//            String imageData = userinfo.getFace();
//
//            // 将查询结果和图片数据都添加到InterviewInvitation对象中
//            interviewInvitation.setUserinfo(userinfo);
//
            //联动学历
            LambdaQueryWrapper<RecruitmentEducation> educationLambdaQueryWrapper = new LambdaQueryWrapper<>();
            educationLambdaQueryWrapper.eq(RecruitmentEducation::getId, userinfo.getRecruitmentId());
            RecruitmentEducation recruitmentEducation = educationMapper.selectOne(educationLambdaQueryWrapper);
            userinfo.setRecruitmentEducation(recruitmentEducation);
            interviewInvitation.setUserinfo(userinfo);
            //联动工作经验
            LambdaQueryWrapper<WorkExperience> workExperienceLambdaQueryWrapper=new LambdaQueryWrapper<>();
            workExperienceLambdaQueryWrapper.eq(WorkExperience::getId,userinfo.getWorkexperienceId());
            userinfo.setWorkExperience( workExperienceMapper.selectOne(workExperienceLambdaQueryWrapper));
            interviewInvitation.setUserinfo(userinfo);

            //求职状态联表查询
            LambdaQueryWrapper<State> stateLambdaQueryWrapper=new LambdaQueryWrapper<>();
            stateLambdaQueryWrapper.eq(State::getId,userinfo.getStateId());
            userinfo.setState(stateMapper.selectOne(stateLambdaQueryWrapper));
            interviewInvitation.setUserinfo(userinfo);
            //期望职位
            LambdaQueryWrapper<Position> positionLambdaQueryWrapper=new LambdaQueryWrapper<>();
            positionLambdaQueryWrapper.eq(Position::getId,userinfo.getPositionId());
            userinfo.setPosition(positionMapper.selectOne(positionLambdaQueryWrapper));
            interviewInvitation.setUserinfo(userinfo);
            //期望薪资
            LambdaQueryWrapper<ExpectedSalary> expectedSalaryLambdaQueryWrapper=new LambdaQueryWrapper<>();
            expectedSalaryLambdaQueryWrapper.eq(ExpectedSalary::getId,userinfo.getPositionId());
            userinfo.setExpectedSalary(expectedSalaryMapper.selectOne(expectedSalaryLambdaQueryWrapper));
            interviewInvitation.setUserinfo(userinfo);
            //求职类型表
            LambdaQueryWrapper<NatureWork> natureWorkLambdaQueryWrapper=new LambdaQueryWrapper<>();
            natureWorkLambdaQueryWrapper.eq(NatureWork::getId,userinfo.getNatureWork());
            userinfo.setNatureWork(natureWorkMapper.selectOne(natureWorkLambdaQueryWrapper));
            interviewInvitation.setUserinfo(userinfo);
            //查询区
            LambdaQueryWrapper<Address> addressLambdaQueryWrapper=new LambdaQueryWrapper<>();
            addressLambdaQueryWrapper.eq(Address::getId,userinfo.getAddressId());
            Address address=addressMapper.selectOne(addressLambdaQueryWrapper);
            userinfo.setAddress(address);
            //查询市
            LambdaQueryWrapper<Address> cityLambadaQueryWrapper=new LambdaQueryWrapper<>();
            cityLambadaQueryWrapper.eq(Address::getId,address.getParentId());
            Address city=addressMapper.selectOne(cityLambadaQueryWrapper);
            userinfo.setCity(city);
        });
        return interviewInvitations;

    }
}
